package com.projet.whattowatch.activity;

import java.util.ArrayList;

import com.projet.whattowatch.R;
import com.projet.whattowatch.nav.NavDrawerItem;
import com.projet.whattowatch.nav.NavDrawerListAdapter;
import com.projet.whattowatch.objet.Utilisateur;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;

public class ConnectedActivity extends FragmentActivity
{
	private DrawerLayout mDrawerLayout;
	private ListView mDrawerList;
	private ActionBarDrawerToggle mDrawerToggle;

	private CharSequence mDrawerTitle;

	private CharSequence mTitle;

	private String[] navMenuTitles;
	private TypedArray navMenuIcons;

	private ArrayList<NavDrawerItem> navDrawerItems;
	private NavDrawerListAdapter adapter;
	
	public static Utilisateur mUtilisateur;
	final String EXTRA_EMAIL = "user_email";

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.connected_layout);

		Intent intent = getIntent();
		if(intent != null)
		{
			String email = intent.getStringExtra(EXTRA_EMAIL);
			for(int i = 0 ; i < MainActivity.mLibUtilisateur.getlistUtilisateur().size(); i++ )
			{
				if (MainActivity.mLibUtilisateur.getlistUtilisateur().get(i).getAdresseMail().equals(email))
				{
					mUtilisateur = MainActivity.mLibUtilisateur.getlistUtilisateur().get(i);
				}
			}
		}
		
		mTitle = mDrawerTitle = getTitle();

		navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
		navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);

		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		mDrawerList = (ListView) findViewById(R.id.list_slidermenu);

		navDrawerItems = new ArrayList<NavDrawerItem>();
		
		// Profil
		navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
		// Acceuil
		navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
		// SmartRequest
		navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
		// Recherche un film
		navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));
		// Recherche une personne
		navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
		// Recherche un cinema
		navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1)));
		// Catégories
		navDrawerItems.add(new NavDrawerItem(navMenuTitles[6], navMenuIcons.getResourceId(6, -1)));
		
		// Recycle the typed array
		navMenuIcons.recycle();

		mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

		// Cration de l'adapteur
		adapter = new NavDrawerListAdapter(getApplicationContext(), navDrawerItems);
		mDrawerList.setAdapter(adapter);

		getActionBar().setDisplayHomeAsUpEnabled(true);
		getActionBar().setHomeButtonEnabled(true);

		mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, 
						R.drawable.ic_drawer, R.string.app_name, R.string.app_name) 
		{
			@Override
			public void onDrawerClosed(View view)
			{
				getActionBar().setTitle(mTitle);
			}

			@Override
			public void onDrawerOpened(View drawerView)
			{
				getActionBar().setTitle(mDrawerTitle);
			}
		};
		
		mDrawerLayout.setDrawerListener(mDrawerToggle);

		if (savedInstanceState == null)
		{
			displayView(1);
		}
	}

	private class SlideMenuClickListener implements ListView.OnItemClickListener
	{
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id)
		{
			displayView(position);
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		if (mDrawerToggle.onOptionsItemSelected(item))
		{
			return true;
		}
		switch (item.getItemId())
		{
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void displayView(int position)
	{
		Fragment fragment = null;
		switch (position) {
		case 0:
			fragment = new ProfilFragment();
			break;
		case 1:
			fragment = new DiscoverFragment();
			break;
		case 2:
			fragment = new SmartRequestFragment();
			break;
		case 3:
			fragment = new SearchMovieFragment();
			break;
		case 4:
			fragment = new SearchPersonFragment();
			break;
		case 5:
			fragment = new SearchCinemaFragment();
			break;
		case 6:
			fragment = new CategoryFragment();
			break;
		default:
			break;
		}

		if (fragment != null)
		{
			FragmentManager fragmentManager = getSupportFragmentManager();
			fragmentManager.beginTransaction()
					.replace(R.id.frame_container, fragment).commit();

			mDrawerList.setItemChecked(position, true);
			mDrawerList.setSelection(position);
			setTitle(navMenuTitles[position]);
			mDrawerLayout.closeDrawer(mDrawerList);
		}
	}

	@Override
	public void setTitle(CharSequence title)
	{
		mTitle = title;
		getActionBar().setTitle(mTitle);
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState)
	{
		super.onPostCreate(savedInstanceState);
		mDrawerToggle.syncState();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig)
	{
		super.onConfigurationChanged(newConfig);
		mDrawerToggle.onConfigurationChanged(newConfig);
	}
	
	@Override
    public void onBackPressed()
    {
		//On instancie notre layout en tant que View
        LayoutInflater factory = LayoutInflater.from(this);
        final View alertDialogView = factory.inflate(R.layout.alert_dialog, null);

        AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
 
        //On affecte la vue personnalisé que l'on a crée à notre AlertDialog
        alertDialog.setView(alertDialogView);
 
        alertDialog.setTitle("Quitter");

        alertDialog.setPositiveButton("Oui", new DialogInterface.OnClickListener() 
        {
            @Override
			public void onClick(DialogInterface dialog, int which) 
            {
            	finish();
            }
        });
 
        alertDialog.setNegativeButton("Non", new DialogInterface.OnClickListener() 
        {
            @Override
			public void onClick(DialogInterface dialog, int which) 
            {
            	dialog.cancel();
            }
        });
        
        alertDialog.show();
    }
}